package com.heima.wemedia.controller.v1;

import com.heima.apis.wemedia.WmNewsControllerApi;
import com.heima.common.constans.wemedia.WemediaContans;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.model.media.pojos.WmNews;
import com.heima.model.media.vo.WmNewsVo;
import com.heima.wemedia.feign.ChannelFeign;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private ChannelFeign channelFeign;


    @PostMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }
    @GetMapping("/findAllChannel")
    @Override
    public ResponseResult findAllChannel(){
        return channelFeign.findAllChannel();
    }


    @PostMapping("/submit")
    @Override
    public ResponseResult summitNews(@RequestBody  WmNewsDto wmNews) {
        return wmNewsService.saveNews(wmNews,wmNews.getStatus());
    }

    @Override
    @GetMapping("/one/{id}")
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.findById(id);
    }

    @Override
    @GetMapping("/del_news/{id}")
    public ResponseResult delById(@PathVariable("id") Integer id) {
        return wmNewsService.delById(id);
    }
    @PostMapping("/down_or_up")
    @Override
    public ResponseResult updateEnabledStatus(@RequestBody WmNewsDto dto) {
        return wmNewsService.updateEnabledStatus(dto);
    }

    /**
     * 更新文章的审核状态
     * @param wmNews
     * @return
     */
    @PostMapping("/update_status")
    public ResponseResult updateNewsStatus(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询需要发布的文章id列表
     *
     * @return
     */
    @GetMapping("/findRelease")
    @Override
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }


    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/findList")
    @Override
    public PageResponseResult findList(@RequestBody NewsAuthDto dto){
        return wmNewsService.findList(dto);
    }

    /**
     * 查询文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/find_news_vo/{id}")
    @Override
    public WmNewsVo findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }
}
